import { failure ,success} from "@/static/js/alert.js";
import instance from "../../url/community.js";


const update_user_text = async (data) => {
    return await instance.post(`/write/user_text`, data, {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    }).then((response) => {
        console.log(response)
        if (response == 1) {
            success("上传成功!")
        } else { 
            failure("上传失败!")
        }
    })
    
}

const get_text = async ({ kind, limit, visibility, issift}) => {
    // limit源自于后端的设置。
    return await instance.get(`/get/text/kind?kind=${kind}&limit=${limit}&visibility=${visibility}&issift=${issift}`)
    .then((response) => {
        if (response == 500) {
            failure("请求失败!")
        } else { 
            return response
        }
    })
}
const  get_text_by_user = async (user_id, limit) => {
     return await instance.get(`/get/text/by_user?user_id=${user_id}&limit=${limit}`)
    .then((response) => {
        if (response == 500) {
            failure("请求失败!")
        } else { 
            return response
        }
    })
}

const get_text_by_id = async (id, userid) => {
    
    if (!userid) { userid = 0}
    return await instance.get(`/get/text/by_id?id=${id}&userid=${userid}`)
        .then((response) => {
        if (response == 500) {
            failure("请求失败!")
        } else { 
            return response
        }
    })
}

const change_text_like = async ({ article_id, author, user, type = 1 }) => {
    
    return await instance.post(`/set/text/user/like?article_id=${article_id}&author=${author}&type=${type}&user=${user}`)
        .then((response) => {
        if (response == 500) {
             return 0
        } else { 
            return response
        }
    })
}

const change_text_collect= async ({article_id,author,user,type=1}) => {
    return await instance.post(`/set/text/user/collect?article_id=${article_id}&author=${author}&type=${type}&user=${user}`)
    .then((response) => {
        if (response == 500) {
           return 0
        } else { 
            return response
        }
    })
}
const change_text_reads = async (article_id ) => {
    return await instance.post(`/set/text/read?article_id=${article_id}`)
        .then((response) => {
        if (response == 500) {
            return 0
        } else { 
            return response
        }
    })
    
}

const get_text_comments_first = async (article_id, userid,limit) => {
    if (!limit) { limit = 1 }
    if (!userid) { userid = 0 }
    return await instance.get(`/get/text/comments/first?article_id=${article_id}&limit=${limit}&userid=${userid}`)
        .then((response) => {
        if (Array.isArray(response)&& response.length == 0) { 
                // success("没有更多了")
                return response
        }
         if (response == 500|| response == 0) {
             failure("请求失败!")
             return []
        } else { 
            return response
        }
    })
}
const get_text_comments_second = async (article_id, f_author, userid,f_uuid, limit) => {
    // 避免undefined
     if (!limit) { limit = 1 }
    if (!userid) { userid = 0 }
    return await instance.get(`/get/text/comments/second?article_id=${article_id}&f_author=${f_author}&f_uuid=${f_uuid}&limit=${limit}&userid=${userid}`)
        .then((response) => {
            if (Array.isArray(response)&& response.length == 0) { 
                // success("没有更多了")
                return response
        }
         if (response == 500|| response == 0) {
            //  failure("请求失败!")
             return []
        } else { 
            return response
        }
    })
}
const put_text_comments_first = async (article_id,author,text) => {
    return await instance.post(`/put/text/comments/first?article_id=${article_id}&author=${author}&text=${text}`
    ).then((response) => {
        if (response == 500 || response == 0) {
            // failure("请求失败!")
        } else { 
            return response
        }
    })
}

const put_text_comments_second = async (article_id, f_author,author, text,f_uuid,
                                        double_two) => {
    return await instance.post(`/put/text/comments/second?article_id=${article_id}&f_author=${f_author}&author=${author}&text=${text}&f_uuid=${f_uuid}&double_two=${double_two}`).then((response) => {
        if (response == 500 || response == 0) {
            // failure("请求失败!")
        } else { 
            return response
        }
    })
}


const set_text_comments_f_like = async (comment_uuid, user, type) => {
     if(type==undefined) { type = 1 }
    return await instance.post(`/set/text/comments/first/like?comment_uuid=${comment_uuid}&user=${user}&type=${type}`
    ).then((response) => {
        return response
    })
}
const set_text_comments_s_like = async (comment_uuid, user, type) => {
    if(type==undefined) { type = 1 }
    return await instance.post(`/set/text/comments/second/like?comment_uuid=${comment_uuid}&user=${user}&type=${type}`
    ).then((response) => { 
        return response
    })
}
const get_text_by_keywords = async (keywords, userid=0,limit=1) => {
    if (!userid) { userid = 0 }
    return await instance.get(`/get/text/by_keywords?keywords=${keywords}&userid=${userid}&limit=${limit}`)
        .then((response) => {
            console.log(response)
            if (response!=undefined,response.length==0) { 
                return response
            }
                 if (response == 500 || response == 0) {
                        failure("请求失败!")
                    } else { 
                        return response
                }
            
       
    })
}

const get_texts_by_user_id_keys = async (
    user_id,
    selection = {},
    keywords='',
    limit = 1,
   
) => {
    return await instance.post(`/get/text/user/baseinfo/selection?user_id=${user_id}&limit=${limit}&keywords=${keywords}`, {selection})
        .then((response) => {
                return response
        })
}

const fix_text_by_id= async (user_id, article_id,type=0) => {
    return await instance.post(`/fix/text/user/id?user_id=${user_id}&article_id=${article_id}&type=${type}`)
    .then((response) => {
        return response
    })

}
const get_text_self_by_id = async (article_id) => {
    // 这个只是为了方便对文章信息的管理
    return await instance.get(`/get/text/user/self/id?article_id=${article_id}`)
        .then((response) => {
            return response
        })
}
const get_comments = async (user_id, selection = {}, keywords, limit = 1) => { 
    // 这个和直接获取文章评论不一样,这个是用于用户管理
    return await instance.post(`/get/user/comments/selections?user_id=${user_id}&keywords=${keywords}&limit=${limit}`, { selection })
        .then((response) => { 
           return response
        })
}
const delete_comment_by_id = async (user_id, comment_id) => { 
    return await instance.post(`/delete/user/comments/id?user_id=${user_id}&comment_id=${comment_id}`)
        .then((response) => { 
            return  response
        })
}

export { 
   
    update_user_text, get_text,
    get_text_by_user, get_text_by_id,
    change_text_like, change_text_collect, change_text_reads,
    get_text_comments_first, get_text_comments_second,
    put_text_comments_first, put_text_comments_second,
    set_text_comments_f_like, set_text_comments_s_like,
    get_text_by_keywords, get_texts_by_user_id_keys, fix_text_by_id, get_text_self_by_id,
    get_comments,delete_comment_by_id
  

} 